<?php defined('SYSPATH') or die('No direct script access.');

/**
 * @file postmeta.php
 * @brief postmeta管理
 * @author Lionel Tsai
 * @version 1.0
 * @date 2012-02-17
 */
class Controller_Postmeta extends Controller_Rest {
	public function before()
	{
		parent::before();
	}

	/**
	 * @brief 获取postmeta
	 *
	 * @param $meta_id
	 *
	 * @return $this
	 */
	public function action_get($meta_id = NULL)
	{
		if($meta_id) $this->get_postmeta($meta_id);
		else $this->get_postmetas();
	}
	
	/**
	 * @brief 获取指定postmeta
	 *
	 * @param $meta_id
	 *
	 * @return $this
	 */
	private function get_postmeta($meta_id)
	{
		$postmeta = ORM::factory('postmeta', $meta_id);
		if(!$postmeta->meta_id)
		{
			$this->response->body('the postmeta not exists')->status(206);
			return;
		}
		$res = json_encode($postmeta->as_array());
		$this->response->body($res)->status(200);
	}
	
	/**
	 * @brief 获取指定起止位置的postmeta
	 *
	 * @return	$this 
	 */
	private function get_postmetas()
	{
		/** 获取起止数*/
		$start = filter_input(INPUT_GET, 'start', FILTER_VALIDATE_INT);
		$pos = filter_input(INPUT_GET, 'pos', FILTER_VALIDATE_INT);

		/** 获取指定文章id*/
		$post_id = filter_input(INPUT_GET, 'post_id');

		$postmeta = ORM::factory('postmeta');
		$start = ($start) ? $start : 0;
		$pos = ($pos) ? $pos : PHP_INT_MAX;
		
		if($post_id)
		{/** 获取指定post_id的postmeta*/
			$postmetas = $postmeta
				->where('post_id','=',$post_id)
				->offset($start)
				->limit($pos)
				->find_all();
		}
		else
		{/** 获取所有时间内的post*/
			$postmetas = $postmeta
					->offset($start)
					->limit($pos)
					->find_all();
		}

		if(!$postmetas->count())
		{
			$this->response->body('postmeta not exists!')->status(202);
		}
		else
		{
			$res['num'] = $postmetas->count();
			$res['list'] = array();
			foreach($postmetas as $pst)
			{
				$res['list'][] = $pst->as_array();
			}
			$json_res = json_encode($res);
			$this->response->body($json_res)->status(200);
		}
	}
	
	/**
	 * @brief add postmeta
	 *
	 * @return $this
	 */
	public function action_post()
	{
		$postmeta = ORM::factory('postmeta');
		if(!$_POST)
		{
			$this->response->body('have no post data!')->status(206);
			return;
		}
		if(!isset($_POST['post_id']))
		{
			$this->response->body('have no post_id!')-status(202);
			return;
		}
		if(isset($_POST['meta_id']))
		{
			unset($_POST['meta_id']);
		}
		try{
			/** 匹配对应的post*/
			$post = ORM::factory('post',$_POST['post_id']);
			if(!$post->ID)
			{
				$this->response->body('post not match!')->status(202);
				return;
			}
			$postmeta->values($_POST)->check()->save();
			$this->response->body($_POST['meta_key'].' add success!')->status(200);
		}
		catch(ORM_Validation_Exception $e)
		{
			$errors = $e->errors();
			foreach($errors as $k=>$v)
			{
				$this->response->body($k.':not available')->status(206);
			}
		}
	}
	
	/**
	 * @brief update postmeta
	 *
	 * @param $meta_id
	 *
	 * @return $this
	 */
	public function action_put($meta_id)
	{
		$postmeta=ORM::factory('postmeta',$meta_id);
		if(!$postmeta->meta_id)
		{
			$this->response->body($meta_id.'postmeta not exists!')->status(206);
			return;
		}
		$new_post = $this->request->post();
		if(!$new_post)
		{
			$this->response->body('no postmeta content post!')->status(206);
			return;
		}

		$post_id = $this->request->post('post_id');
		if(!$post_id)
		{
			$this->response->body('have no post_id!')->status(206);
			return;
		}

		try
		{
			$post_id = $this->request->post('post_id');
			/** 匹配对应的post*/
			$post = ORM::factory('post',$post_id);
			if(!$post->ID)
			{
				$this->response->body('post not match!')->status(202);
				return;
			}
			$postmeta->values($new_post)->check()->save();
			$this->response->body($this->request->post('meta_key').' add success!')->status(200);
		}
		catch(ORM_Validation_Exception $e)
		{
			$errors = $e->errors();
			foreach($errors as $k=>$v)
			{
				$this->response->body($k.':not available')->status(206);
			}
		}
		catch(Exception $err)
		{
			$this->response->body($err->getMessage())->status(500);
		}
	}
	
	/**
	 * @brief delete postmeta
	 *
	 * @param $meta_id
	 *
	 * @return $this
	 */
	public function action_delete($meta_id)
	{
		$postmeta = ORM::factory('postmeta', $meta_id);
		if(!$postmeta->meta_id)
		{
			$this->response->body('postmeta not exist!')->status(206);
			return;
		}
		try
		{
			$postmeta->delete();
			$this->response->body('"'.'postmeta-'.$meta_id." delete success!")->status(200);
		}
		catch(Exception $e)
		{
			$this->response->body($e->getMessage())->status(500);
		}
	}
}
